package com.zhuozhou.jdyc.main.main.viewmodel;

import android.app.Application;
import android.content.Context;
import android.os.Bundle;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.MutableLiveData;

import com.zhuozhou.jdyc.api.RegexUtils;
import com.zhuozhou.jdyc.api.UserAccountHelper;
import com.zhuozhou.jdyc.base.BaseViewModel;
import com.zhuozhou.jdyc.main.base.activity.WebViewActivity;
import com.zhuozhou.jdyc.main.goods.bean.BasicsCommodityBase;
import com.zhuozhou.jdyc.main.main.bean.BasicsCitySearchBean;
import com.zhuozhou.jdyc.main.main.bean.BasicsCommodityTypeBean;
import com.zhuozhou.jdyc.main.main.bean.BasicsShoppingBean;
import com.zhuozhou.jdyc.main.main.bean.BasicsTypeBean;
import com.zhuozhou.jdyc.main.main.bean.BasicsTypeDetailsBean;
import com.zhuozhou.jdyc.main.main.bean.BasicsTypeListBean;
import com.zhuozhou.jdyc.main.main.bean.BasicsTypePost;
import com.zhuozhou.jdyc.main.main.bean.BasicsYiHuoStopBean;
import com.zhuozhou.jdyc.main.main.bean.ChopCommodityTopBean;
import com.zhuozhou.jdyc.main.main.bean.CouponItemBean;
import com.zhuozhou.jdyc.main.main.bean.IndexDetailsBean;
import com.zhuozhou.jdyc.main.main.bean.IndexDetailsPost;
import com.zhuozhou.jdyc.main.main.bean.JiFenCommodityTopBean;
import com.zhuozhou.jdyc.main.main.bean.ShopCommodityListBean;
import com.zhuozhou.jdyc.main.main.bean.ShopIndexBean;
import com.zhuozhou.jdyc.main.main.view.MainView;
import com.zhuozhou.jdyc.main.main.wight.CustomTabTextView;
import com.zhuozhou.jdyc.main.meinfo.bean.ShippingAddressBean;
import com.zhuozhou.jdyc.main.settlement.bean.PayBean;
import com.zhuozhou.jdyc.main.settlement.bean.PayInfoBean;
import com.zhuozhou.jdyc.main.settlement.bean.VerificationBean;
import com.zhuozhou.jdyc.main.user.bean.IndexMeInfo;
import com.zhuozhou.jdyc.util.apiUtil.StringUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Create by CherishTang on 2020/3/19 0019
 * describe:
 */
public class MainViewModel extends BaseViewModel<MainView> {

    private CustomTabTextView mCurrentTabTextView;
    public final static String ARG_PAGE = "page";

    private Fragment mCurrentFragment;

    public MainViewModel(@NonNull Application application) {
        super(application);
        setShowDialog(false);
    }

    /**
     * 处理二维码扫码结果
     *
     * @param context 视图
     * @param result  结果
     */
    public void decodeQRCodeResult(Context context, Object result) {
        if (StringUtil.isEmpty(result)) {
            return;
        }
        if (RegexUtils.isUrl(result.toString())) {
            WebViewActivity.show(context, result.toString(), "");
        } else {
            baseView.showToast("扫码结果为：" + result);
        }
    }


    public void doSelect(FragmentManager mFragmentManager, @IdRes int containerViewId, CustomTabTextView newTabTextView) {
        CustomTabTextView oldTabTextView = null;
        if (mCurrentTabTextView != null) {
            oldTabTextView = mCurrentTabTextView;
            if (oldTabTextView == newTabTextView) {
                onReselect(oldTabTextView);
                return;
            }
            oldTabTextView.changeBackground(false);
        }
        newTabTextView.changeBackground(true);
        doTabChanged(mFragmentManager, containerViewId, oldTabTextView, newTabTextView);
        mCurrentTabTextView = newTabTextView;
    }

    private void doTabChanged(FragmentManager mFragmentManager, @IdRes int containerViewId, CustomTabTextView oldTabTextView, CustomTabTextView newTabTextView) {
        FragmentTransaction ft = mFragmentManager.beginTransaction();
        if (oldTabTextView != null) {
            if (oldTabTextView.getFragment() != null) {
                ft.hide(oldTabTextView.getFragment());
            }
        }

        if (newTabTextView != null) {
            if (newTabTextView.getFragment() == null) {
                Fragment fragment = Fragment.instantiate(getApplication(),
                        newTabTextView.getClx().getName(), newTabTextView.getBundle());
                ft.add(containerViewId, fragment, newTabTextView.getTag());
                newTabTextView.setFragment(fragment);
            } else {
                ft.show(newTabTextView.getFragment());
            }
        }
        ft.commit();
    }

    private void onReselect(CustomTabTextView tabTextView) {
        if (listener != null) {
            listener.onReselect(tabTextView);
        }
    }

    private OnCustomTabTextViewReselectListener listener;

    private void setOnCustomTabTextViewReselectListener(OnCustomTabTextViewReselectListener listener) {
        this.listener = listener;
    }

    interface OnCustomTabTextViewReselectListener {
        void onReselect(CustomTabTextView tabTextView);
    }


    public void setFragment(FragmentManager mFragmentManager, Fragment fragment, @IdRes int id) {
        if (fragment == null) {
            return;
        }
        if (mCurrentFragment != fragment) {
            if (fragment.isAdded()) {
                mFragmentManager.beginTransaction().hide(mCurrentFragment).show(fragment).commit();
            } else {
                mFragmentManager.beginTransaction().replace(id, fragment).commit();
            }
            mCurrentFragment = fragment;
        }
    }

    /**
     * 创建bundle
     *
     * @param page
     * @param bd
     * @return
     */
    public Bundle createBundle(int page, Bundle bd) {
        Bundle bundle = new Bundle();
        bundle.putInt(ARG_PAGE, page);
        if (bd != null)
            bundle.putAll(bd);
        return bundle;
    }

    public MutableLiveData<BasicsTypeBean> basicsTypeMutableLiveData = new MutableLiveData<>();

    public void getBasicsType(String city, int page, int type) {
        observe(apiServiceHelper.getBasicsType(new BasicsTypePost(city, type, page)), false, basicsTypeMutableLiveData);
    }

    public MutableLiveData<BasicsTypeBean> basicsTypeMutableLiveDataIntegral = new MutableLiveData<>();

    public void getBasicsTypeIntegral(String city, int page, int type) {
        observe(apiServiceHelper.getBasicsType(new BasicsTypePost(city, type, page)), false, basicsTypeMutableLiveDataIntegral);
    }

    public MutableLiveData<List<ShopIndexBean>> shopIndexMutableLiveData = new MutableLiveData<>();

    public void getShopIndex(int page, Boolean isShowDialog) {
        observe(apiServiceHelper.getShopIndex(new BasicsTypePost(UserAccountHelper.getNewCity(), page, 31.8512, 117.26061)), isShowDialog, shopIndexMutableLiveData);
    }

    public MutableLiveData<List<ChopCommodityTopBean>> chopCommodityTopMutableLiveData = new MutableLiveData<>();

    public void getChopCommodityTop() {
        observe(apiServiceHelper.getChopCommodityTop(new BasicsTypePost(UserAccountHelper.getNewCity())), false, chopCommodityTopMutableLiveData);
    }

    public MutableLiveData<BasicsCommodityTypeBean> basicsCommodityTypeBeanMutableLiveData = new MutableLiveData<>();

    public void getBasicsCommodityType(Boolean isShowDialog, int type, int page) {
        observe(apiServiceHelper.getBasicsCommodityType(new BasicsTypePost(UserAccountHelper.getNewCity(), type, page)), isShowDialog, basicsCommodityTypeBeanMutableLiveData);
    }

    public MutableLiveData<IndexDetailsBean> shopIndexDetailsBeanMutableLiveData = new MutableLiveData<>();

    public void getShopIndexDetails(String id, String user_id, int type) {
        observe(apiServiceHelper.getShopIndexDetails(new IndexDetailsPost(id, user_id, type)), true, shopIndexDetailsBeanMutableLiveData);
    }

    public MutableLiveData<List<BasicsCitySearchBean>> shopBasicsCitySearchMutableLiveData = new MutableLiveData<>();

    public void getBasicsCitySearch(String city) {
        observe(apiServiceHelper.getBasicsCitySearch(new BasicsTypePost(city, 1)), true, shopBasicsCitySearchMutableLiveData);
    }

    public MutableLiveData<ShopCommodityListBean> shopCommodityListMutableLiveData = new MutableLiveData<>();

    public void getShopCommodityList(String city) {
        observe(apiServiceHelper.getShopCommodityList(new BasicsTypePost(city)), true, shopCommodityListMutableLiveData);
    }

    public MutableLiveData<ShopCommodityListBean> shopCommodityDetailMutableLiveData = new MutableLiveData<>();

    public void getShopCommodityDetail(String city, int id) {
        BasicsTypePost post = new BasicsTypePost(city);
        post.setId(id);
        observe(apiServiceHelper.getShopCommodityDetail(post), true, shopCommodityDetailMutableLiveData);
    }

    public MutableLiveData<ShopCommodityListBean> basicsSearchAdminMutableLiveData = new MutableLiveData<>();

    public void getBasicsSearchAdmin(String city, String shoptype, int type_id) {
        Map map = new HashMap();
        map.put("city", city);
        map.put("shoptype", shoptype);
        map.put("type_id", type_id);
        observe(apiServiceHelper.getBasicsSearchAdmin(map), true, basicsSearchAdminMutableLiveData);
    }

    public MutableLiveData<BasicsCommodityTypeBean> basicsSearchMutableLiveData = new MutableLiveData<>();

    public void getBasicsSearch(String city, String shoptype, int type, int type_id) {
        Map map = new HashMap();
        map.put("city", city);
        map.put("shoptype", shoptype);
        map.put("type", type);
        map.put("type_id", type_id);
        observe(apiServiceHelper.getBasicsSearch(map), true, basicsSearchMutableLiveData);
    }

    public MutableLiveData<BasicsTypeListBean> BasicsTypeListMutableLiveData = new MutableLiveData<>();

    public void getBasicsTypeList(String city, int type) {
        Map map = new HashMap();
        map.put("city", city);
        map.put("type", type);
        observe(apiServiceHelper.getBasicsTypeList(map), true, BasicsTypeListMutableLiveData);
    }

    public MutableLiveData<BasicsTypeDetailsBean> BasicsTypeDetailsMutableLiveData = new MutableLiveData<>();

    public void getBasicsTypeDetails(String city, int type, int id) {
        Map map = new HashMap();
        map.put("city", city);
        map.put("type", type);
        map.put("id", id);
        observe(apiServiceHelper.getBasicsTypeDetails(map), true, BasicsTypeDetailsMutableLiveData);
    }

    public MutableLiveData<BasicsCommodityTypeBean> BasicsTypeDetails2MutableLiveData = new MutableLiveData<>();

    public void getBasicsTypeDetails2(String city, int type, int id) {
        Map map = new HashMap();
        map.put("city", city);
        map.put("type", type);
        map.put("id", id);
        observe(apiServiceHelper.getBasicsTypeDetails2(map), true, BasicsTypeDetails2MutableLiveData);
    }

    public MutableLiveData<JiFenCommodityTopBean> JiFenCommodityTopMutableLiveData = new MutableLiveData<>();

    public void getJiFenCommodityTop(String city) {
        Map map = new HashMap();
        map.put("city", city);
        observe(apiServiceHelper.getJiFenCommodityTop(map), true, JiFenCommodityTopMutableLiveData);
    }

    public MutableLiveData<List<BasicsYiHuoStopBean>> BasicsYiHuoStopMutableLiveData = new MutableLiveData<>();

    public void getBasicsYiHuoStop(String city) {
        Map map = new HashMap();
        map.put("city", city);
        observe(apiServiceHelper.getBasicsYiHuoStop(map), true, BasicsYiHuoStopMutableLiveData);
    }

    public MutableLiveData<BasicsShoppingBean> BasicsShoppingMutableLiveData = new MutableLiveData<>();

    public void getBasicsShopping(String type, int user_id) {
        Map map = new HashMap();
        map.put("type", type);
        map.put("user_id", user_id);
        observe(apiServiceHelper.getBasicsShopping(map), true, BasicsShoppingMutableLiveData);
    }

    public MutableLiveData<BasicsCitySearchBean> BasicsFollowMutableLiveData = new MutableLiveData<>();

    public void getBasicsFollow(String admin_id, int user_id) {
        Map map = new HashMap();
        map.put("admin_id", admin_id);
        map.put("user_id", user_id);
        observe(apiServiceHelper.getBasicsFollow(map), true, BasicsFollowMutableLiveData);
    }

    public MutableLiveData<IndexMeInfo> BasicsStatisticsMutableLiveData = new MutableLiveData<>();

    public void getBasicsStatistics(String user_id) {
        Map map = new HashMap();
        map.put("user_id", user_id);
        observe(apiServiceHelper.getBasicsStatistics(map), true, BasicsStatisticsMutableLiveData);
    }

    public MutableLiveData<CouponItemBean> shopSelectCouponsMutableLiveData = new MutableLiveData<>();

    public void postShopSelectCoupons(int admin_id, int discount_id, int user_id) {
        Map map = new HashMap();
        map.put("admin_id", admin_id);
        map.put("discount_id", discount_id);
        map.put("user_id", user_id);
        observe(apiServiceHelper.postShopSelectCoupons(map), true, shopSelectCouponsMutableLiveData);
    }

    //timetable/Basics/mySite
    public MutableLiveData<ShippingAddressBean> MySiteMutableLiveData = new MutableLiveData<>();

    public void getMySite(String user_id) {
        Map map = new HashMap();
        map.put("user_id", user_id);
        observe(apiServiceHelper.getMySite(map), true, MySiteMutableLiveData);
    }

    //timetable/Basics/distribution
    public MutableLiveData<List<BasicsCommodityBase.DataBean.DistributionBean>> DistributionMutableLiveData = new MutableLiveData<>();

    public void getDistribution(String admin_id) {
        Map map = new HashMap();
        map.put("admin_id", admin_id);
        observe(apiServiceHelper.getDistribution(map), true, DistributionMutableLiveData);
    }

    public MutableLiveData<CouponItemBean> BasicsShoppingCountMutableLiveData = new MutableLiveData<>();

    public void postShoppingCount(String admin_id, int commodity_id, int number, String sku, String type, int user_id) {
        Map map = new HashMap();
        map.put("admin_id", admin_id);
        map.put("commodity_id", commodity_id);
        map.put("number", number);
        map.put("sku", sku);
        map.put("type", type);
        map.put("user_id", user_id);
        observe(apiServiceHelper.postShoppingCount(map), false, BasicsShoppingCountMutableLiveData);
    }

    public MutableLiveData<BasicsCitySearchBean> BasicsDelShoppingMutableLiveData = new MutableLiveData<>();

    public void postDelShopping(String store_id, int commodity_id, String sku, String type, int user_id) {
        Map map = new HashMap();
        map.put("store_id", store_id);
        map.put("commodity_id", commodity_id);
        map.put("sku", sku);
        map.put("type", type);
        map.put("user_id", user_id);
        observe(apiServiceHelper.postDelShopping(map), true, BasicsDelShoppingMutableLiveData);
    }

    //timetable/Basics/mypay
    public MutableLiveData<VerificationBean> MyPayMutableLiveData = new MutableLiveData<>();

    public void getMyPay(int user_id) {
        Map map = new HashMap();
        map.put("user_id", user_id);
        observe(apiServiceHelper.getMyPay(map), true, MyPayMutableLiveData);
    }

    //timetable/Basics/hexiao
    public MutableLiveData<BasicsCitySearchBean> HeXaoMutableLiveData = new MutableLiveData<>();

    public void getHeXao(String out_trade_no) {
        Map map = new HashMap();
        map.put("out_trade_no", out_trade_no);
        observe(apiServiceHelper.getHeXao(map), false, HeXaoMutableLiveData);
    }

    //timetable//Basics/pay
    public MutableLiveData<PayInfoBean> BasicsPayMutableLiveData = new MutableLiveData<>();

    // obj: obj,
//                    sourceId: app.globalData.sourceId,
    public void toPay(PayBean obj, int sourceId) {
        Map map = new HashMap();
        map.put("obj", obj);
        map.put("sourceId", sourceId);
        observe(apiServiceHelper.toPay(map), true, BasicsPayMutableLiveData);
    }

}
